	
global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A38_country_clustering.log, replace name(tabl)

capture noi { 
	
	* The baseline table with alternative inference: clustering at country-level and using cluster-bootstrapped p-values
	clear
	qui do ${code_dir}/config/tabletools.do

	* Load the Cameron (2008) p-values
	use ${final_dir}/p_values_cluster.dta, clear
	forvalues col=1/9 {
		local p_`col' = p_value[`col']
	}
	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
	tab year, gen(YY_)

	estimates clear
	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	sort lse_id year


	cap program drop vars
	program vars
	hasvar HSW*, local("hsw") yes("\yes") no("\no")
	hasvar stock* spill*, local("stockspill") yes("\yes") no("\no")
	hasvar GDPGAP*, local("gdpgap") yes("\yes") no("\no")
	hasvar VAEMP*, local("vaemp") yes("\yes") no("\no")
	hasvar GDPPC*, local("gdppc") yes("\yes") no("\no")
	end program

	cap program drop pstat
	program pstat
		args pstat
		assert "`pstat'" != ""
		mat p2 = e(b)
		mat p2[1,1] = `pstat'
		estadd matrix p2
	end program

	* 1) GDPGAP | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster country_shr_1995)
	pstat `p_1'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_1

	* 2) GDPGAP + VAEMP | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster country_shr_1995)
	pstat `p_2'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_2

	* 3) GDPGAP + GDPPC | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster country_shr_1995)
	pstat `p_3'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_3

	* 4) GDPGAP | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster country_shr_1995)
	pstat `p_4'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_4

	* 5) GDPGAP + VAEMP | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster country_shr_1995)
	pstat `p_5'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_5

	* 6) GDPGAP + GDPPC | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster country_shr_1995)
	pstat `p_6'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_6

	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a	
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPGAP_foreign = lngdpgap_shr_foreign_1995_a
	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

	* 7) GDPGAP | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster country_shr_1995)
	pstat `p_7'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_7

	* 8) GDPGAP + VAEMP | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster country_shr_1995)
	pstat `p_8'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_8

	* 9) GDPGAP + GDPPC | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign GDPPC_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster country_shr_1995)
	pstat `p_9'
	vars
	preserve 
	keep if e(sample)
	duplicates drop BvD, force
	qui count 
	restore
	estadd local nr_firms "{\num{`r(N)'}}"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_9

	setlabels
	dhoztab * using ${tab_dir}/appendix/Table_A38_country_clustering.tex, collabels(none) cells(b(star fmt(%10.2f)) se(fmt(%10.2f) par) p(fmt(%10.3f) par([ ])) p2(fmt(%10.3f) par(\{ \}))) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_hsw High-skill wage" "has_gdpgap GDP gap" "has_vaemp Labor productivity" "has_gdppc GDP per capita" "has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "nr_firms Firms") label drop(stock* spill* HSW GDPGAP VAEMP GDPPC _cons) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPGAP_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) ///
		depvar("Auto95") ///
		notes("This table reproduces the baseline table using different inference procedures. The standard errors in parentheses are clustered at country-level (instead of firm-level). The [ ] brackets report the associated p-values. To account for few clusters, the \{ \} brackets report cluster-bootstrapped p-values following Cameron et. al (2008).")
	*again, brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl